<?php
namespace App\Http\Controllers\Admin;

use App\admin\Good;
use App\admin\Category;
use App\Http\Controllers\Admin\LogHander;
use App\Http\Controllers\Controller;
use App\Http\Middleware;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Routing\Controller as BaseController;

class GoodsController extends BaseController
{
	public function __construct()
	{
		$this->middleware('admindo');
	}
	//详情
	public function index(Request $request)
	{
		$Good = new Good;
		$id = $request->input('id');
		if($id == ""){
			return "更新出错，找不到产品id";
		}
		$temp = $Good::find($id);
		return view('admin.goodsdetial',['data'=>$temp]);
	}
	//列表
	public function list_()
	{
		$Good = new Good;
		$flights = $Good::orderBy('id', 'desc')->paginate(15);
		return view('admin.goodlist',['data' => $flights]);
	}
	
	public function add()
	{
		//取产品分类
		$cate = Category::orderBy('id', 'desc')->get();
		return view('admin.goodsadd',['cat'=>$cate]);
	}
	//上传资料
	public function addup(Request $request)
	{
		$newName = $request->input('newName');
		$selt = $request->input('selt');
		$Description = $request->input('newDescription');
		$content = $request->input('content');
		
		if($newName == "" || $Description=="" ||$content==""){
			return "请输入产品名称、描述、内容";
		}
		$pathBaseOne = "";
		$pathBaseTwo = "";
		if ($request -> hasFile('fileOne')) {
			$file = $request -> file('fileOne');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/goods/'. $name;
			Image::make($file) -> save($path);
			$pathBaseOne = 'upload/goods/'. $name;
		}else{
			return "请选择产品导读图片";
		}
		
//		$name = date("Ymd").str_random(10) .".pdf";
		if ($request -> hasFile('fileTwo')) {
			$getPDFpath = $request->file('fileTwo')->store("pdf");//上传名  + 目录名
			if($getPDFpath != ""){
				$pathBaseTwo = $getPDFpath;
			}
		}
		
//		if ($request -> hasFile('fileTwo')) {
//			$file = $request -> file('fileTwo');
//			$entension = $file -> getClientOriginalExtension();
//			if($entension != "pdf"){
//				return redirect()->back();
//			}
//			$name = date("Ymd").str_random(10) .".". $entension;
//			$path = public_path() .'/upload/goods/'. $name;
//			Image::make($file) -> save($path);
//			$pathBaseTwo = 'upload/goods/'. $name;
//		}else{
//			return "请选择PDF文件";
//		}
		//---------------------------------------
		$Good = new Good;
		$Good->name = $newName;
		$Good->desctiption = $Description;
		$Good->info = $content;
		
		$Good->pdf = $pathBaseTwo;
		$Good->imgurl = $pathBaseOne;
		$Good->type = $selt;
		$Good->state = 0;
		if($Good->save()){
			$cars=array('name'=>'GoodsUp','info'=>'添加产品','cnt'=>$newName);
			$temp = new LogHander;
			$temp->logs($cars);
			return redirect('Goodslist');
		}
		
	}
	
	public function updateup(Request $request)
	{
		$Good = new Good;
		$id = $request->input('id');
		if($id == ""){
			return "更新出错，找不到产品id";
		}
		$newName = $request->input('newName');
		$selt = $request->input('selt');
		$Description = $request->input('newDescription');
		$content = $request->input('content');
		$pathBaseOne = "";$pathBaseTwo = "";
		if ($request -> hasFile('fileOne')) {
			$file = $request -> file('fileOne');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/goods/'. $name;
			Image::make($file) -> save($path);
			$pathBaseOne = 'upload/goods/'. $name;
		}
		if ($request -> hasFile('fileTwo')) {
			$getPDFpath = $request->file('fileTwo');//上传名  + 目录名
	//		return $getPDFpath->name;
			$pathBaseTwo = $getPDFpath->store("pdf");
		}
		//---------------------------------------
		$temp = $Good::find($id);
		$temp->name = $newName;
		$temp->desctiption = $Description;
		$temp->info = $content;
		
		$temp->pdf = $pathBaseTwo;
		$temp->imgurl = $pathBaseOne;
		$temp->type = $selt;
		$temp->state = 0;
		if($temp->save()){
			$cars=array('name'=>'GoodsUpdate','info'=>'编辑产品','cnt'=>$newName);
			$temp = new LogHander;
			$temp->logs($cars);
			return redirect('Goodslist');
		}
	}
	
	public function edite(Request $request)
	{
		
		$id = $request->input('id');
		$cate = Category::orderBy('id', 'desc')->get();
		$Good = new Good;
		if($id == ""){
			return "参数出错";
		}
		$temp = $Good::find($id);
		return view('admin.goodsedite',['cat'=>$cate,'data'=>$temp]);
	}
	
	public function del(Request $request)
	{
		$id = $request->input('id');
		$Good = new Good;
		$data = $Good::find($id);
		
		$cars=array('name'=>'Goodsdel','info'=>'删除产品','cnt'=>$data->name."---".$data->description);
		$temp = new LogHander;
		$temp->logs($cars);
		
//		$data->state = 1;
		if($data->delete()){
			return "ok";
		}else{
			return "no";
		}
	}
	
	public function upDown(Request $request){
		$id = $request->input('id');
		$Good = new Good;
		$data = $Good::find($id);
		if($data->state ==1){
			$data->state = 0;
		}else{
			$data->state = 1;
		}
		if($data->save()){
			return "ok";
		}else{
			return "no";
		}
	}
}
